/*     */ package com.zimbra.cs.account.ldap.upgrade;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Version;
/*     */ import com.zimbra.cs.account.AttributeCardinality;
/*     */ import com.zimbra.cs.account.AttributeClass;
/*     */ import com.zimbra.cs.account.AttributeInfo;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.util.BuildInfo;
/*     */ import java.util.Collection;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class BUG_27075
/*     */   extends UpgradeOp
/*     */ {
/*     */   private Version mSince;
/*     */   
/*     */   boolean parseCommandLine(CommandLine cl)
/*     */   {
/*  50 */     String[] args = cl.getArgs();
/*  51 */     if ((args == null) || (args.length != 1)) {
/*  52 */       LdapUpgrade.usage(null, this, "missing required argument: since");
/*  53 */       return false;
/*     */     }
/*     */     try
/*     */     {
/*  57 */       this.mSince = new Version(args[0]);
/*     */     } catch (ServiceException e) {
/*  59 */       LdapUpgrade.usage(null, this, "invalid version: " + args[0]);
/*  60 */       return false;
/*     */     }
/*  62 */     return true;
/*     */   }
/*     */   
/*     */   void usage(HelpFormatter helpFormatter)
/*     */   {
/*  67 */     this.printer.println();
/*  68 */     this.printer.println("args for bug " + this.bug + ":");
/*  69 */     this.printer.println("    {since}  (e.g. 5.0.12)");
/*  70 */     this.printer.println();
/*     */   }
/*     */   
/*     */   void doUpgrade() throws ServiceException
/*     */   {
/*  75 */     ZLdapContext zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
/*     */     try {
/*  77 */       doGlobalConfig(zlc);
/*  78 */       doAllCos(zlc);
/*     */     } finally {
/*  80 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private String formatMultiValue(Collection<String> values)
/*     */   {
/*  88 */     StringBuilder sb = new StringBuilder();
/*  89 */     boolean first = true;
/*  90 */     for (String v : values) {
/*  91 */       if (!first)
/*  92 */         sb.append(", ");
/*  93 */       sb.append(v);
/*  94 */       first = false;
/*     */     }
/*  96 */     return sb.toString();
/*     */   }
/*     */   
/*     */   private boolean needsUpgrade(AttributeManager am, String attr) throws ServiceException {
/* 100 */     String since = this.mSince.toString();
/*     */     
/* 102 */     if ((!am.beforeVersion(attr, since)) && (!am.isFuture(attr))) {
/* 103 */       return true;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 122 */     if (am.addedIn(attr, "5.0.17")) {
/* 123 */       boolean fromATroubledInstall = (this.mSince.compare("6.0.0_BETA1") == 0) || (this.mSince.compare("6.0.0_BETA2") == 0);
/* 124 */       if (fromATroubledInstall) {
/* 125 */         return true;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 146 */     if ("zimbraFreebusyExchangeServerType".equalsIgnoreCase(attr)) {
/* 147 */       boolean fromATroubledInstall = this.mSince.compare("7.0.0") == 0;
/* 148 */       if (fromATroubledInstall) {
/* 149 */         return true;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 159 */     if ("zimbraMailEmptyFolderBatchThreshold".equalsIgnoreCase(attr)) {
/* 160 */       boolean fromATroubledInstall = (this.mSince.compare("7.0.0") >= 0) && (this.mSince.compare("7.1.1") < 0);
/*     */       
/* 162 */       if (fromATroubledInstall) {
/* 163 */         return true;
/*     */       }
/*     */     }
/*     */     
/* 167 */     return false;
/*     */   }
/*     */   
/*     */   private void doEntry(ZLdapContext zlc, Entry entry, String entryName, AttributeClass klass) throws ServiceException
/*     */   {
/* 172 */     this.printer.println();
/* 173 */     this.printer.println("------------------------------");
/* 174 */     this.printer.println("Upgrading " + entryName + ": ");
/*     */     
/* 176 */     AttributeManager am = AttributeManager.getInstance();
/*     */     
/* 178 */     Set<String> attrs = am.getAttrsInClass(klass);
/* 179 */     Map<String, Object> attrValues = new HashMap();
/* 180 */     for (String attr : attrs) {
/* 181 */       AttributeInfo ai = am.getAttributeInfo(attr);
/* 182 */       if (ai != null)
/*     */       {
/*     */ 
/* 185 */         List<Version> attrVersion = ai.getSince();
/*     */         
/* 187 */         if (needsUpgrade(am, attr)) {
/* 188 */           if (this.verbose) {
/* 189 */             this.printer.println("");
/* 190 */             this.printer.println("Checking " + entryName + " attribute: " + attr + attrVersion);
/*     */           }
/*     */           
/* 193 */           String curVal = entry.getAttr(attr);
/* 194 */           if (curVal != null)
/*     */           {
/* 196 */             if (this.verbose) {
/* 197 */               if (ai.getCardinality() == AttributeCardinality.multi)
/* 198 */                 curVal = formatMultiValue(entry.getMultiAttrSet(attr));
/* 199 */               this.printer.println("    skipping - already has value: " + curVal);
/*     */ 
/*     */ 
/*     */ 
/*     */             }
/*     */             
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */           }
/*     */           else
/*     */           {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 216 */             List<String> values = null;
/* 217 */             if (klass == AttributeClass.globalConfig) {
/* 218 */               values = ai.getGlobalConfigValuesUpgrade();
/* 219 */               if (values == null)
/* 220 */                 values = ai.getGlobalConfigValues();
/* 221 */             } else if (klass == AttributeClass.cos) {
/* 222 */               values = ai.getDefaultCosValuesUpgrade();
/* 223 */               if (values == null)
/* 224 */                 values = ai.getDefaultCosValues();
/*     */             } else {
/* 226 */               this.printer.println("Internal error: invalid attribute class " + klass.name());
/* 227 */               return;
/*     */             }
/*     */             
/* 230 */             if ((values == null) || (values.size() == 0)) {
/* 231 */               if (this.verbose) {
/* 232 */                 this.printer.println("    skipping - does not have a default value");
/*     */               }
/*     */             }
/*     */             else
/*     */             {
/* 237 */               attrValues.clear();
/* 238 */               if (ai.getCardinality() != AttributeCardinality.multi) {
/* 239 */                 this.printer.println("    setting " + entryName + " attribute " + attr + attrVersion + " to: " + (String)values.get(0));
/* 240 */                 attrValues.put(attr, values.get(0));
/*     */               } else {
/* 242 */                 this.printer.println("    setting " + entryName + " attribute " + attr + attrVersion + " to: " + formatMultiValue(values));
/* 243 */                 attrValues.put(attr, values.toArray(new String[0]));
/*     */               }
/*     */               try
/*     */               {
/* 247 */                 modifyAttrs(zlc, entry, attrValues);
/*     */               }
/*     */               catch (ServiceException e) {
/* 250 */                 this.printer.println("Caught ServiceException while modifying " + entryName + " attribute " + attr);
/* 251 */                 this.printer.printStackTrace(e);
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       } } }
/*     */   
/* 258 */   private void doBug38425(Entry entry, String entryName) { String theAttr = "zimbraPrefMailDefaultCharset";
/* 259 */     String sinceVer = this.mSince.toString();
/* 260 */     String thisVer = BuildInfo.VERSION;
/*     */     
/* 262 */     if ((sinceVer.startsWith("6.0.0_BETA1")) && (thisVer.startsWith("6.0.0_BETA2"))) {
/* 263 */       String curVal = entry.getAttr(theAttr);
/* 264 */       if ("UTF-8".equalsIgnoreCase(curVal)) {
/* 265 */         HashMap<String, Object> attrs = new HashMap();
/* 266 */         attrs.put(theAttr, "");
/*     */         try {
/* 268 */           this.printer.println("Unsetting " + theAttr + " on " + entryName);
/* 269 */           this.prov.modifyAttrs(entry, attrs);
/*     */         } catch (ServiceException e) {
/* 271 */           this.printer.println("Caught ServiceException while unsetting " + theAttr + " on " + entryName);
/* 272 */           this.printer.printStackTrace(e);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void doBug79208(Config config)
/*     */     throws ServiceException
/*     */   {
/* 281 */     boolean stepRequired = (this.mSince.compare("7.2.3") < 0) || ((this.mSince.compare("8.0.0") >= 0) && (this.mSince.compare("8.0.3") < 0));
/* 282 */     if (!stepRequired) {
/* 283 */       return;
/*     */     }
/*     */     
/*     */ 
/* 287 */     HashMap<String, Object> attrs = new HashMap();
/* 288 */     String attr = "zimbraHttpThreadPoolMaxIdleTimeMillis";
/* 289 */     String curVal = config.getAttr(attr, null);
/* 290 */     if (curVal == null) {
/* 291 */       attrs.put(attr, Integer.valueOf(config.getHttpThreadPoolMaxIdleTimeMillis()));
/*     */     }
/* 293 */     attr = "zimbraHttpConnectorMaxIdleTimeMillis";
/* 294 */     curVal = config.getAttr(attr, null);
/* 295 */     if (curVal == null) {
/* 296 */       attrs.put(attr, Integer.valueOf(config.getHttpConnectorMaxIdleTimeMillis()));
/*     */     }
/* 298 */     if (!attrs.isEmpty()) {
/*     */       try {
/* 300 */         this.printer.println("Setting " + attrs.keySet() + " on globalConfig");
/* 301 */         this.prov.modifyAttrs(config, attrs);
/*     */       } catch (ServiceException e) {
/* 303 */         this.printer.println("Caught ServiceException while setting " + attrs.keySet() + " on globalConfig");
/* 304 */         this.printer.printStackTrace(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void doBug83551(Config config)
/*     */     throws ServiceException
/*     */   {
/* 312 */     boolean stepRequired = (this.mSince.compare("7.2.5") < 0) || ((this.mSince.compare("8.0.0") >= 0) && (this.mSince.compare("8.0.5") < 0));
/* 313 */     if (!stepRequired) {
/* 314 */       return;
/*     */     }
/*     */     
/*     */ 
/* 318 */     HashMap<String, Object> attrs = new HashMap();
/* 319 */     String attr = "zimbraWebGzipEnabled";
/* 320 */     String curVal = config.getAttr(attr, null);
/* 321 */     if (curVal == null) {
/* 322 */       attrs.put(attr, ("" + config.isWebGzipEnabled()).toUpperCase());
/*     */     }
/*     */     
/* 325 */     attr = "zimbraHttpCompressionEnabled";
/* 326 */     curVal = config.getAttr(attr, null);
/* 327 */     if (curVal == null) {
/* 328 */       attrs.put(attr, ("" + config.isHttpCompressionEnabled()).toUpperCase());
/*     */     }
/*     */     
/* 331 */     if (!attrs.isEmpty()) {
/*     */       try {
/* 333 */         this.printer.println("Setting " + attrs.keySet() + " on globalConfig");
/* 334 */         this.prov.modifyAttrs(config, attrs);
/*     */       } catch (ServiceException e) {
/* 336 */         this.printer.println("Caught ServiceException while setting " + attrs.keySet() + " on globalConfig");
/* 337 */         this.printer.printStackTrace(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void doGlobalConfig(ZLdapContext zlc) throws ServiceException {
/* 343 */     Config config = this.prov.getConfig();
/* 344 */     doEntry(zlc, config, "global config", AttributeClass.globalConfig);
/* 345 */     doBug79208(config);
/* 346 */     doBug83551(config);
/*     */   }
/*     */   
/*     */   private void doAllCos(ZLdapContext zlc) throws ServiceException {
/* 350 */     List<Cos> coses = this.prov.getAllCos();
/*     */     
/* 352 */     for (Cos cos : coses) {
/* 353 */       String name = "cos " + cos.getName();
/* 354 */       doEntry(zlc, cos, name, AttributeClass.cos);
/* 355 */       doBug38425(cos, name);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/upgrade/BUG_27075.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */